package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationVo;
import com.zzyl.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理微信app")
public class CustomerReservationController extends BaseController {
    @Autowired
    private CustomerReservationService customerReservationService;

    @ApiOperation("分页查询预约")
    @GetMapping("/page")
    public ResponseResult<PageResponse<ReservationVo>> getByPage(
            @ApiParam(value = "当前页码")
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量")
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @ApiParam(value = "状态")
            @RequestParam(value = "status", required = false) Integer status) {
        PageResponse<ReservationVo> pageInfo = customerReservationService.selectByPage(pageNum, pageSize, status);
        return success(pageInfo);
    }

    @ApiOperation("新增预约")
    @PostMapping
    public ResponseResult add(@RequestBody ReservationVo reservationVo) {
        customerReservationService.add(reservationVo);
        return success();
    }

    @ApiOperation("取消预约")
    @PutMapping("/{id}/cancel")
    public ResponseResult CancelAppointment(@PathVariable Long id) {
        customerReservationService.cancelAppointment(id);
        return ResponseResult.success();
    }

    @ApiOperation("查询取消预约数量")
    @GetMapping("/cancelled-count")
    public ResponseResult getCancelledCount() {
        Integer count = customerReservationService.cancelCount();
        System.out.println("============查询取消预约数量================"+count);
        return ResponseResult.success(count);
    }

    @ApiOperation("查询每个时间段剩余预约次数")
    @GetMapping("/countByTime")
    public ResponseResult<List<TimeCountVo>> numberRemaining(@RequestParam Long time) {
        List<TimeCountVo> list = customerReservationService.numberRemaining(time);
        return ResponseResult.success(list);
    }
//    @Scheduled(cron = "0 1,31 * * * ?")
    @Scheduled(cron = "0 21,31 * * * ?")
    public void executeTask(){
        customerReservationService.scheduledTasks();
        log.info("定时任务执行成功");
    }

}
